from django.db import models
from django.utils.timezone import now


# Create your models here.

class AdminAccountPassword(models.Model):
    """管理员账户密码表"""
    password = models.CharField(verbose_name='密文密码', max_length=100, blank=False, default='')

    class Meta:
        verbose_name = '管理员密码'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'aoduo_admin_AdminAccountPassword'


class AdminAccount(models.Model):
    """管理员用户表"""
    types = {
        ('1', '总管理员'),
        ('2', '副管理员'),
        ('3', '其他管理员')
    }

    username = models.CharField(verbose_name='管理员登录用户名', max_length=100, default='', blank=False, unique=True)

    email = models.EmailField(verbose_name='邮箱', blank=False, default=None, unique=True)

    password = models.ForeignKey(AdminAccountPassword, verbose_name='密码', default='', blank=False,
                                 on_delete=models.CASCADE)

    register_date = models.DateField(verbose_name='注册时间', default=now, blank=False)

    last_login_time = models.DateTimeField(verbose_name='最后登录时间', default=now)

    admin_type = models.CharField(verbose_name='管理员类别', choices=types, max_length=100, default='3', blank=False)

    active = models.BooleanField(verbose_name='账户激活', default=True, blank=False)

    class Meta:
        verbose_name = '管理员账号'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'aoduo_admin_AdminAccount'
        ordering = ['-register_date']

    def __str__(self):
        return self.username


class AuthType(models.Model):
    """权限类型"""
    name = models.CharField(verbose_name='权限名', max_length=100, default='', blank=False)

    class Meta:
        verbose_name = '权限类型'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'aoduo_admin_AuthType'

    def __str__(self):
        return self.name


class AdminGroup(models.Model):
    """管理员用户组表"""

    group_name = models.CharField(verbose_name='组名', max_length=100, default='', blank=False)

    create_time = models.DateField(verbose_name='创建时间', default=now)

    auth = models.ManyToManyField(AuthType, verbose_name='权限列表', default=None, blank=True)

    group_admin = models.ManyToManyField(AdminAccount, verbose_name='组内管理员', default='', blank=True)

    class Meta:
        verbose_name = '用户组'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'aoduo_admin_AdminGroup'

    def __str__(self):
        return str(self.group_name)


class AdminOperationLog(models.Model):
    """管理员操作记录"""
    related_admin = models.ForeignKey(AdminAccount, verbose_name='关联管理员', default='', on_delete=models.CASCADE,
                                      blank=False)
    related_auth_type = models.ForeignKey(AuthType, verbose_name='关联操作类型', default='', on_delete=models.CASCADE)

    other_message = models.CharField(verbose_name='其他信息', default='', blank=False, max_length=100)

    operation_time = models.DateTimeField(verbose_name='操作时间', default=now)

    class Meta:
        verbose_name = '管理员操作记录'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'aoduo_admin_AdminOperationLog'
        ordering = ['-operation_time']
